package com.css.fxfzaqbz.modules.data.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.modules.data.entity.StratumData;
import com.css.fxfzaqbz.modules.data.queryParams.StratumDataQueryParams;
import com.css.fxfzaqbz.modules.data.service.StratumDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 3.3.1构造环境数据管理子模块 -表3地层数据表单
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class StratumDataController {
    @Resource
    StratumDataService stratumDataService ;

    /**
     * 新增stratumData
     * @param stratumData
     * @return
     */
    @PostMapping("/saveStratumData")
    public RestResponse saveStratumData (@RequestBody StratumData stratumData){
        RestResponse restResponse = null ;
        try{
            restResponse = RestResponse.succeed(stratumDataService.saveStratumData(stratumData));
        }catch (Exception e){
            log.error("新增地层数据失败",e);
            restResponse = RestResponse.fail("新增地层数据失败!");
        }
        return restResponse;
    }

    /**
     * 全查询  模糊查询未设定条件
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllStratumData")
    public RestResponse getAllStratumData(HttpServletRequest request, StratumDataQueryParams queryParams){

        RestResponse restResponse = null;
        try{
//            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
//            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(Integer.parseInt(PlatformPageUtils.CUR_PAGE_DEF));
            queryParams.setPageSize(Integer.parseInt(PlatformPageUtils.PAGE_SIZE_DEF));
            restResponse = RestResponse.succeed(stratumDataService.getAllStratumData(queryParams));
        }catch (Exception e){
            log.error("查询地层数据失败!",e);
            restResponse = RestResponse.fail("查询地层数据失败");
        }
        return restResponse;
    }

    /**
     * 根据id查询StratumData数据
     * @param id
     * @return
     */
    @GetMapping("/getStratumData/{id}")
    public RestResponse<StratumData> getStratumData(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(stratumDataService.getStratumDataById(id));
        }catch (Exception e){
            log.error("获取地层数据失败!",e);
            restResponse = RestResponse.fail("获取地层数据失败!");
        }
        return restResponse;
    }


    /**
     * 逻辑删除，修改数据标志位
     * @param ids
     * @return
     */
    @PostMapping("/delStratumData/{ids}")
    public RestResponse deleteStratumData(@PathVariable("ids") List<String> ids) {
        RestResponse restResponse = null;
        try{
            stratumDataService.deleteStratumDatas(ids);
            restResponse = RestResponse.succeed();
        }catch (Exception e){
            log.error("删除地层数据失败!",e);
            restResponse = RestResponse.fail("删除地层数据失败!");
        }
        return restResponse;
    }

    /**
     * 修改
     * @param stratumData
     * @return
     */
//    @PostMapping("/updateStratumData")
//    public RestResponse<StratumData> updateActiveFaultData(@RequestBody StratumData stratumData)  {
//        RestResponse restResponse = null;
//        try{
//            restResponse = RestResponse.succeed(stratumDataService.updateStratumData(stratumData));
//        }catch (Exception e){
//            log.error("修改地层数据失败!",e);
//            restResponse = RestResponse.fail("修改地层数据失败!");
//        }
//        return restResponse;
//    }

    /**
     * 导出表内数据
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportExcelStratumData")
    public void exportExcel(HttpServletResponse response ) throws IOException {
        RestResponse restResponse = null;
        try{
            List<StratumData> list  = stratumDataService.getAll();
            FileUtil2.exportExcel(list, "地层数据", "sheet", StratumData.class, "StratumData", response);
            restResponse = RestResponse.succeed("导出成功");
        }catch (Exception e){
            log.error("导出失败",e);
            restResponse = RestResponse.fail("导出失败");
        }
    }

    /**
     * 导入数据
     * @param file
     * @throws IOException
     */
    @PostMapping("/importExcelStratumData")
    public RestResponse importExcel(MultipartFile file) throws IOException {
        RestResponse restResponse = null;
        try{
            stratumDataService.importExcel(file);
            restResponse = RestResponse.succeed("导入成功");
        }catch (Exception e){
            log.error("导入失败",e);

        }
        return restResponse ;
    }
}
